<template>
  <a-modal
    :visible="visible"
    :title="$t('all.autotext294')"
    :oktext="$t('all.autotext371')"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form :label-col="{ span: 5 }" :wrapper-col="{ span: 15 }">
      <a-form-item :label="$t('all.autotext1439')">
        <a-month-picker
          v-model="month"
          :disabled-date="disabledDate"
          valueFormat="YYYY-MM"
          :placeholder="$t('all.autotext97')"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import moment from 'moment'
import storage from 'store'
import { CURRENT_PROJECT } from '@/store/mutation-types'
import { exportPDF } from '@/api/project/finance'
import { exportPdf } from '@/utils/util'
export default {
  props: ['visible'],
  data () {
    return {
      proId: '',
      month: '',
      confirmLoading: false
    }
  },
  created () {
    const pd = localStorage.getItem('CURRENT_PROID')
    if (pd) {
      this.proId = parseInt(pd)
    }
  },
  methods: {
    disabledDate (current) {
      const pro = storage.get(CURRENT_PROJECT)
      return current && (current > moment().endOf('month') || current < moment(JSON.parse(pro).createTime).startOf('month'))
    },
    async handleOk () {
      this.confirmLoading = true
      const res = await exportPDF({ proId: this.proId, yMonth: this.month })
      this.$message.success(this.$t('all.autotext556'))
      exportPdf(this.$t('all.autotext868') + this.month, res)
      this.confirmLoading = false
      this.$emit('update:visible', false)
    },
    handleCancel () {
      this.$emit('update:visible', false)
    }
  }
}
</script>
